/**
* 资源订购情况
*/
<style lang="scss" scoped>
  @import "../../utils/base.scss";

  .title_card1 {
    padding-left: 8px;
    float: left;
    width: calc(12.5% - 8.75px);
    height: 80px;
    border: 1px solid #dcdae2;
    text-align: left;
    line-height: 40px;
    font-weight: bold;
    margin-bottom: 10px;
    background: linear-gradient(to bottom right, #288CFF, #83BDFF);
    position: relative;
  }

  .title_card1 {
    cursor: pointer;
  }

  .title_card2 {
    position: relative;
    padding-left: 8px;
    float: left;
    width: calc(12.5% - 8.75px);
    height: 80px;
    border: 1px solid #dcdae2;
    text-align: left;
    line-height: 40px;
    margin-bottom: 10px;
    background: linear-gradient(to bottom right, #43D6A5, #7CE3A8)
  }

  .title_card2:hover {
    cursor: pointer;
  }

  .header_title {
    height: 50px;
    line-height: 32px;
    padding-top: 9px;
    padding-bottom: 9px;
  }

  .title_number {
    font-size: 24px;
    color: #FFFFFF;
  }

  .title_text {
    font-size: 14px;
    color: #FFFFFF;
    position: absolute;
  }

  .image_icon {
    position: absolute;
    bottom: 4px;
    right: 4px;
    opacity: 0.2;
  }
</style>
<template>
  <div>
    <pheader style="border-bottom: 1px solid #dcdae2; ">
      <div slot="left">资源订购情况</div>
    </pheader>
    <tabContent>
      <template slot="content" style="position: relative;">
        <div style="width: 100%;    display: inline-block;">
          <div class="title_card1" @click="filterProjectType([])">
            <div style="width: 100%;position: relative;">
              <p class="title_number">{{tenantGeneral.tenantTotal}}</p>
              <p class="title_text">租户总数</p>
            </div>
            <img src="../../../static/image/resource_1.png" class="image_icon"></image>
          </div>
          <div class="title_card1" @click="filterProjectType([])"
            style="margin-left: 10px;background:linear-gradient(to bottom right,#FF9C03,#FEBD39)">
            <div style="width: 100%;position: relative;">
              <p class="title_number">{{tenantGeneral.projectTotal}}</p>
              <p class="title_text">入驻应用总数</p>
            </div>
            <img src="../../../static/image/resource_3.png" class="image_icon"></image>
          </div>
          <template v-for="(item, index) in tenantGeneral.tenantTypeTotals">
            <div class="title_card2" @click="filterProjectType([item.tenanttype])" :style="{marginLeft: '10px'}">
              <div style="width: 100%;position: relative;">
                <p class="title_number">{{item.total}}</p>
                <p class="title_text">{{item.tenanttype}}</p>
              </div>
              <img src="../../../static/image/resource_2.png" class="image_icon"></image>
            </div>
          </template>

        </div>

          <Form :label-width="64" label-position="right">
        <Row class="header_title">
            <Col span="4" class="col_paddingleft">
            <FormItem label="租户名称" style="margin-bottom: 0px;">
              <Input v-model="searchInfo.tenantName" placeholder="请输入" />
            </FormItem>
            </Col>
            <Col span="4" class="col_paddingleft">
            <FormItem label="项目名称" style="margin-bottom: 0px;">
              <Input v-model="searchInfo.projectName" placeholder="请输入" />
            </FormItem>
            </Col>
            <Col span="14" style="margin-left: 12px;">
            <Button type="text" @click="refresh"><span style="color: #2d8cf0;">
                <Icon type="md-refresh" size="15" style="padding-right:8px;line-height: 8px;" />重置
              </span></Button>
            <Button type="primary" @click="search">
              <Icon type="md-search" size="15" style="padding-right:8px;line-height: 8px;" />查询
            </Button>
            <Button :loading="loadingExport" type="primary" @click="exportData">
              <Icon type="md-cloud-download" size="15" style="padding-right:8px;line-height: 8px;" />导出
            </Button>
            </Col>
        </Row>
          </Form>
        <!-- show-summary sum-text="总计" -->
        <Table ref="table" size="small" :columns="columns" @on-sort-change="sortTime" :data="resourceList" border
          style="margin-right:1px">
          <template slot-scope="{ row, index }" slot="index">
            <span>{{index + 1}}</span>
          </template>
          <template slot-scope="{ row, index }" slot="projectType">
            <span>{{row.project.projectType}}</span>
          </template>
          <template slot-scope="{ row, index }" slot="tenantName">
            <span>{{row.project.tenant_name}}</span>
          </template>
          <template slot-scope="{ row, index }" slot="projectName">
            <span>{{row.project.project}}</span>
          </template>
          <template slot-scope="{ row, index }" slot="suretTime">
            <span>{{row.keyStageTime.suretTime}}</span>
          </template>
          <template slot-scope="{ row, index }" slot="resourceDoneTime">
            <span>{{row.keyStageTime.resourceDoneTime}}</span>
          </template>
          <template slot-scope="{ row, index }" slot="costHours">
            <span>{{row.keyStageTime.costHours >= 0 ? parseFloat(row.keyStageTime.costHours).toFixed(2):'--'}}</span>
          </template>
          <template slot-scope="{ row, index }" slot="action">
            <Button @click="toDetail(row)">查看详情</Button>
          </template>
        </Table>
        <br />
        <Page :total="allResourceList.length" :current="current" :page-size="10"
          style="font-size: 13px; text-align: right; padding-right: 20px;" @on-change="getPageList" prev-text="上一页"
          next-text="下一页" />
        </Col>
        <Spin size="large" fix v-if="loadingSpin"></Spin>
      </template>
    </tabContent>
  </div>
</template>

<script>
  import ajax from "../../ajax_util"
  import pheader from "../../comp_chatter/pheader"
  import tabContent from "../../comp_chatter/tab_content.vue"
  import resourcesJson from '../../../static/resources'
  import excel from "../../utils/excel";

  export default {
    name: "resouceInfo",
    components: {
      pheader,
      tabContent
    },
    data() {
      return {
        loadingExport: false,
        loadingSpin: false,
        tabs: null,
        sortTenantNameType: "desc",
        searchInfo: {
          tenantName: "",
          projectName: ""
        },
        loadingSpin: false,
        allResourceList: [],
        resourceList: [],
        exportColumns: [{
            title: "序号",
            align: "center",
            width: 70,
            key: "index",
          },
          {
            title: "租户类型",
            align: "left",
            maxWidth: 120,
            key: "projectType",
          },
          {
            title: "租户名称",
            align: "left",
            // minWidth: 110,
            key: "tenantName",
          },
          {
            title: "项目名称",
            align: "left",
            // minWidth: 110,
            key: "projectName",
          },
          {
            title: "入驻状态",
            align: "left",
            maxWidth: 110,
            key: "statusDes",
          },
          {
            title: "入驻启动时间",
            align: "left",
            maxWidth: 110,
            key: "suretTime",
          },
          {
            title: "入驻完成时间",
            align: "left",
            maxWidth: 110,
            key: "resourceDoneTime",
          },
          {
            title: "入驻耗时(天)",
            align: "left",
            width: 70,
            key: "costHours",
          },
          {
            title: "运营人员",
            align: "left",
            maxWidth: 110,
            key: "businessManName",
          },
          {
            title: "运营人员组织",
            align: "left",
            maxWidth: 110,
            key: "businessStruct",
          },
          {
            title: "租户接口人",
            align: "left",
            maxWidth: 110,
            key: "projectManager",
          },
          {
            title: '资源种类数',
            align: 'center',
            maxWidth: 80,
            key: "resourceNum"
          },
          {
            title: '服务器总台数',
            align: 'center',
            maxWidth: 80,
            key: "severNum"
          },
          {
            title: 'CPU总数(核)',
            align: 'center',
            maxWidth: 100,
            key: "CpuNum",
          },
          {
            title: '内存总数(GB)',
            align: 'center',
            maxWidth: 90,
            key: "RamNum",
          },
          {
            title: '存储总数(TB)',
            align: 'center',
            maxWidth: 90,
            key: "VolumnNum",
          },
          {
            title: "工具订购总数",
            align: "center",
            maxWidth: 80,
            key: "toolNum"
          },
          {
            title: "数据订购总数",
            align: "center",
            maxWidth: 80,
            key: "dateApiNum"
          },
        ],
        tenantGeneral: {}, //获取的所有概览信息
        current: 1,
        sortType: "desc",
        filterProjectTypeName: "全部",
        statusFilterList: [],
        filterStatus: "全部"
      }
    },
    created: function() {
      this.tabs = this.$store.state.tabs
      this.loadingSpin = true
      this.getAllResourceList()
    },
    computed: {
      columns() {
        var _self = this
        let columns = [];
        columns.push();
        columns.push({
          title: "序号",
          align: "center",
          width: 70,
          key: "index",
          slot: "index",
          // fixed: 'left'
        }, {
          title: "租户类型",
          align: "left",
          minWidth: 100,
          key: "projectType",
          slot: "projectType",
          filterMultiple: false,
          filters: [{
              label: '集团内部',
              value: '集团内部',
            },
            {
              label: '直属单位',
              value: '直属单位'
            },
            {
              label: '省公司',
              value: '省公司',
            },
            {
              label: '专业公司',
              value: '专业公司',
            },

            {
              label: 'IT公司内部',
              value: 'IT公司内部',
            },
            {
              label: '外部合作方',
              value: '外部合作方',
            }
          ],
          filterRemote(value) {
            _self.filterProjectType(value);
          }

        }, {
          title: "租户名称",
          align: "left",
          minWidth: 110,
          key: "tenantName",
          slot: "tenantName",
          renderHeader: (h, index) => {
            return h('div', [
              h('span', "租户名称"),
              h("Icon", {
                props: {
                  type: this.sortTenantNameType == 'asc' ? 'md-arrow-dropdown' : 'md-arrow-dropup',
                  size: 18
                },
                class: "slort_name",
                on: {
                  click: () => {
                    this.slortTenantName()
                  }
                }
              }, "")
            ])
          }
        }, {
          title: "项目名称",
          align: "left",
          minWidth: 110,
          key: "projectName",
          slot: "projectName"
        }, {
          title: "入驻状态",
          align: "left",
          width: 110,
          key: "statusDes",
          filterMultiple: false,
          filters: _self.statusFilterList,
          filterRemote(value) {
            console.log("this，---->", _self)
            _self.filterByStatus(value);
          }
        }, {
          title: "入驻启动时间",
          align: "left",
          width: 105,
          key: "suretTime",
          slot: "suretTime",
        }, {
          title: "入驻完成时间",
          align: "left",
          width: 105,
          key: "resourceDoneTime",
          slot: "resourceDoneTime",
          sortable: 'custom'
        }, {
          title: "入驻耗时(天)",
          align: "left",
          width: 70,
          key: "costHours",
          slot: "costHours"
        }, {
          title: "资源订购情况",
          align: "center",
          children: [{
              title: '资源种类数',
              align: 'center',
              width: 80,
              key: "resourceNum"
            },
            {
              title: '服务器总台数',
              align: 'center',
              width: 80,
              key: "severNum"
            },
            {
              title: 'CPU总数(核)',
              align: 'center',
              width: 100,
              key: "CpuNum",
            },
            {
              title: '内存总数(GB)',
              align: 'center',
              width: 90,
              key: "RamNum",
            },
            {
              title: '存储总数(TB)',
              align: 'center',
              width: 90,
              key: "VolumnNum",
            },
          ]
        }, {
          title: "工具订购总数",
          align: "center",
          width: 80,
          key: "toolNum"
        }, {
          title: "数据订购总数",
          align: "center",
          width: 80,
          key: "dateApiNum"
        }, {
          title: "操作",
          align: "center",
          width: 119.1,
          // fixed: 'right',
          slot: "action"
        });
        return columns;
      }
    },
    mounted: function() {},
    methods: {
      exportData() {
        this.loadingExport = true
        this.getResourceList()
      },
      getResourceList() {
        var data = {
          resourceList: [],
          severList: [],
          toolList: []
        }
        this.allResourceList.forEach(item => {
          item.resources.forEach(resource => {
            if (data.resourceList.find(e => e.serviceId == resource.serviceId) == undefined) {
              data.resourceList.push(resource)
            }
          })
          item.tools.forEach(tool => {
            if (data.toolList.find(e => e.toolId == tool.toolId) == undefined) {
              data.toolList.push(tool)
            }
          })
          item.servers.forEach(server => {
            if (data.severList.find(e => e.severId == server.severId) == undefined) {
              data.severList.push(server)
            }
          })
        })

        var resouceList = this.sortResouce(data.resourceList)
        var severList = JSON.parse(JSON.stringify(data.severList))
        var toolList = JSON.parse(JSON.stringify(data.toolList))
        this.downFile(resouceList, toolList, severList)
      },
      downFile(resouceList, toolList, severList) {
        var option = {
          body: [],
          name: '资源订购列表(' + this.filterProjectTypeName + ")_" + new Date().getTime(),
          suffix: 'xlsx',
          merges: []
        }
        var row1 = []
        var row2 = []
        var row3 = []
        this.exportColumns.forEach((item, index) => {
          row1.push(item.title)
          row2.push(item.title)
          row3.push(item.title)
          option.merges.push({
            s: {
              r: 0,
              c: index
            },
            e: {
              r: 2,
              c: index
            }
          })
        })

        var indexList = [] // 记录资源所在的数组下标
        //设置资源表头
        if (resouceList.length > 0) {
          resouceList.forEach(item1 => {
            option.merges.push({
              s: {
                r: 0,
                c: row1.length
              },
              e: {
                r: 0,
                c: row1.length + item1.serviceName.length - 1
              }
            })
            item1.servieList.forEach(item2 => {
              option.merges.push({
                s: {
                  r: 1,
                  c: row2.length
                },
                e: {
                  r: 1,
                  c: row2.length + item2.subName.length - 1
                }
              })
              item2.subName.forEach(item3 => {
                var clazz = item1.clazz.split(".").length > 1 ? item1.clazz.split(".")[1] : item1.clazz
                var serviceName = item2.serviceName.split(".").length > 1 ? item2.serviceName.split(".")[
                  1] : item2.serviceName
                var subName = item3.subName.split(".").length > 1 ? item3.subName.split(".")[1] : item3
                  .subName
                if (item3.resourcetype == 0 || item3.resourcetype == 1 || item3.resourcetype == 3) {
                  if (item3.unit == null || item3.unit == "") {
                    subName += "(" + item3.options.join("/") + ")";
                  } else {
                    subName += "(" + item3.unit + ")";
                  }
                } else if (item3.resourcetype == 4) {
                  subName += "(" + item3.options.join("/") + ")";
                } else if (item3.resourcetype == 5) {
                  subName += "(双副本;单位：" + item3.unit + ")";
                } else if (item3.resourcetype == 6) {
                  subName += "(三副本;单位：" + item3.unit + ")";
                }

                console.log("item3----->", item3)
                row1.push(clazz)
                row2.push(serviceName)
                row3.push(subName)
                indexList.push({
                  serviceId: item3.serviceId,
                  toolId: -1,
                  severId: -1,
                  index: row3.length - 1
                })
              })
            })
          })
        }
        // 处理工具表头
        if (toolList.length > 0) {
          option.merges.push({
            s: {
              r: 0,
              c: row1.length
            },
            e: {
              r: 0,
              c: row1.length + toolList.length - 1
            }
          })

          toolList.forEach((item, index) => {
            option.merges.push({
              s: {
                r: 1,
                c: row3.length
              },
              e: {
                r: 2,
                c: row3.length
              }
            })
            row1.push("大数据工具清单")
            row2.push(item.toolName)
            row3.push(item.toolName)
            indexList.push({
              serviceId: -1,
              toolId: item.toolId,
              severId: -1,
              index: row3.length - 1
            })
          })
        }
        //设置服务器表头
        if (severList.length > 0) {
          // 处理物理机
          option.merges.push({
            s: {
              r: 0,
              c: row1.length
            },
            e: {
              r: 0,
              c: row1.length + severList.length - 1
            }
          })
          severList.forEach((item, index) => {
            option.merges.push({
              s: {
                r: 1,
                c: row3.length
              },
              e: {
                r: 2,
                c: row3.length
              }
            })
            console.log("--->", item)
            row1.push("云服务器")
            row2.push(item.service.severName + "(台)")
            row3.push(item.service.severName + "(台)")
            indexList.push({
              serviceId: -1,
              toolId: -1,
              severId: item.severId,
              index: row3.length - 1
            })
          })
        }


        option.body.push(row1)
        option.body.push(row2)
        option.body.push(row3)
        if (this.allResourceList.length > 0) {

          this.allResourceList.forEach((item, index) => {
            var arr = new Array(row3.length)
            arr[0] = index + 1
            arr[1] = item.project.projectType
            arr[2] = item.project.tenant_name
            arr[3] = JSON.stringify(item.project.project) // .replace('\r\n', '')
            arr[4] = item.statusDes
            arr[5] = item.keyStageTime.suretTime
            arr[6] = item.keyStageTime.resourceDoneTime
            arr[7] = item.keyStageTime.costHours >= 0 ? parseFloat(item.keyStageTime.costHours).toFixed(2) : '--'
            arr[8] = item.businessManName
            arr[9] = JSON.stringify(item.businessStruct)
            arr[10] = item.projectManager
            arr[11] = item.resourceNum
            arr[12] = item.severNum
            arr[13] = this.sumNumber(item.resourceCpuNum, item.severCpuNum).toFixed(2)

            arr[14] = this.sumNumber(item.resourceRamNum, item.severRamNum).toFixed(2)
            arr[15] = this.sumNumber(item.resourceVolumnNum, item.severVolumnNum).toFixed(2)
            arr[16] = item.toolNum
            arr[17] = item.dateApiNum
            item.resources.forEach(resource => {
              var i = indexList.find(e => e.serviceId == resource.serviceId).index
              var otherInfo = ""
              console.log("otherInfo---->", resource, otherInfo, resource.resourcetype, resource.val)
              if (resource.resourcetype == 4) {
                otherInfo = resource.val.indexOf("[") != -1 ? JSON.parse(resource.val).join("、") : JSON.parse(
                  resource.val)
              } else {
                otherInfo = resource.val
              }
              if (arr[i] != undefined) {
                if (resource.resourcetype == 0 || resource.resourcetype == 5 || resource.resourcetype == 6) {
                  arr[i] = parseFloat(arr[i]) + parseFloat(otherInfo) + ""
                } else {
                  arr[i] += " / " + otherInfo
                }
              } else {
                arr[i] = otherInfo
              }
            })
            item.tools.forEach(tool => {
              var i = indexList.find(e => e.toolId == tool.toolId).index
              if (arr[i] != undefined) {
                arr[i] += "是"
              } else {
                arr[i] = "是"
              }
            })
            item.servers.forEach(server => {
              var i = indexList.find(e => e.severId == server.severId).index

              if (arr[i] != undefined) {
                arr[i] = parseInt(arr[i]) + parseInt(server.quantity) + ""
              } else {
                arr[i] = server.quantity
              }
            })

            option.body.push(arr)
          })
        }

        console.log("option---->", option)
        excel.exportFromArray(option)
        this.loadingExport = false
      },
      sortResouce(list) {
        var temp = list.sort((a, b) => {
          let i = a['clazz'].localeCompare(b['clazz'])
          if (i == 0) {
            return a['serviceName'].localeCompare(b['serviceName'])
          } else {
            return i
          }
        })
        return this.megerResouce(temp)
      },
      megerResouce(list) {
        var merges = []
        //处理第一层 clazz归类
        list.forEach((item, index) => {
          var clazz = merges.find(e => e.clazz == item.clazz)
          if (clazz != undefined) {
            clazz.serviceName.push(item)
          } else {
            merges.push({
              clazz: item.clazz,
              serviceName: [item]
            })
          }
        })
        // 处理第二层
        merges.forEach(item => {
          var merges2 = []
          item.serviceName.forEach(service => {
            var clazz = merges2.find(e => e.serviceName == service.serviceName)
            if (clazz != undefined) {
              clazz.subName.push(service)
            } else {
              merges2.push({
                serviceName: service.serviceName,
                subName: [service]
              })
            }
          })
          this.$set(item, "servieList", merges2)
        })
        console.log("merges1---->", merges)
        return merges
      },

      getAllResourceList() {
        // var t = JSON.stringify(resourcesJson)
        // var data2 = JSON.parse(t)
        // var that = this
        // that.statusFilterList = []
        // 	data2.list.forEach((item, index) => {
        // 		if (that.statusFilterList.find(e => e.label == item.statusDes) == undefined) {
        // 		that.statusFilterList.push({
        // 			label: item.statusDes,
        // 			value: item.statusDes,
        // 		})
        // 	}
        // 	if (item.project === null) {
        // 		data2.list.splice(index, 1)
        // 	} else {
        // 		if (item.keyStageTime.resourceDoneTime != null && item.keyStageTime.resourceDoneTime > 0) {
        // 			item.keyStageTime.resourceDoneTime = that.formaTime(item.keyStageTime.resourceDoneTime * 1000)
        // 		} else {
        // 			item.keyStageTime.resourceDoneTime = "--"
        // 		}
        // 		if (item.keyStageTime.suretTime != null && item.keyStageTime.suretTime > 0 ) {
        // 			item.keyStageTime.suretTime = that.formaTime(item.keyStageTime.suretTime * 1000)
        // 		} else {
        // 			item.keyStageTime.suretTime = "--"
        // 			if (item.project.create_time != null && item.project.create_time > 0) {
        // 				item.keyStageTime.resourceDoneTime = that.formaTime(item.project.create_time * 1000)
        // 			}
        // 		}
        // 		if (item.project.create_time != null && item.project.create_time > 0) {
        // 			item.project.create_time = that.formaTime(item.project.create_time * 1000)
        // 		} else {
        // 			item.project.create_time = ""
        // 		}
        // 		if (item.keyStageTime.reqDoneTime != null && item.keyStageTime.reqDoneTime > 0) {
        // 			item.keyStageTime.reqDoneTime = that.formaTime(item.keyStageTime.reqDoneTime * 1000)
        // 		} else {
        // 			item.keyStageTime.reqDoneTime = "--"
        // 		}
        // 	}
        // 	item.resourceVolumnNum = parseFloat(item.resourceVolumnNum / 1024).toFixed(2);
        // 	item.severVolumnNum = parseFloat(item.severVolumnNum / 1024).toFixed(2);
        // 	item.CpuNum = that.sumNumber(item.resourceCpuNum, item.severCpuNum).toFixed(2)
        // 	item.RamNum = that.sumNumber(item.resourceRamNum, item.severRamNum).toFixed(2)
        // 	item.VolumnNum = that.sumNumber(item.resourceVolumnNum, item.severVolumnNum).toFixed(2)
        // })
        // // that.sortTitle(data2)
        // // that.sortDataByTenantName(that.allResourceList)
        // that.sortDataByTime(data2.list)
        // var t = JSON.stringify(data2)
        // var temp = JSON.parse(t)
        // that.tenantGeneral = data2
        // that.allResourceList = temp.list
        // that.getPageList(1)
        // that.loadingSpin = false
        var url = this.CONFIG.progress_url + "/statistic/tenant-general"
        var that = this;
        ajax.get({
          url: url,
          that: that,
          success: function(data) {
            var t = JSON.stringify(data)
            var data2 = JSON.parse(t)
            that.statusFilterList = []
            data2.list.forEach((item, index) => {
              if (that.statusFilterList.find(e => e.label == item.statusDes) == undefined) {
                that.statusFilterList.push({
                  label: item.statusDes,
                  value: item.statusDes,
                })
              }
              if (item.project === null) {
                data2.list.splice(index, 1)
              } else {
                if (item.keyStageTime.resourceDoneTime != null && item.keyStageTime.resourceDoneTime > 0) {
                  item.keyStageTime.resourceDoneTime = that.formaTime(item.keyStageTime.resourceDoneTime *
                    1000)
                } else {
                  item.keyStageTime.resourceDoneTime = "--"
                }
                if (item.keyStageTime.suretTime != null && item.keyStageTime.suretTime > 0) {
                  item.keyStageTime.suretTime = that.formaTime(item.keyStageTime.suretTime * 1000)
                } else {
                  item.keyStageTime.suretTime = "--"
                  if (item.project.create_time != null && item.project.create_time > 0) {
                    item.keyStageTime.resourceDoneTime = that.formaTime(item.project.create_time * 1000)
                  }
                }
                if (item.project.create_time != null && item.project.create_time > 0) {
                  item.project.create_time = that.formaTime(item.project.create_time * 1000)
                } else {
                  item.project.create_time = ""
                }
                if (item.keyStageTime.reqDoneTime != null && item.keyStageTime.reqDoneTime > 0) {
                  item.keyStageTime.reqDoneTime = that.formaTime(item.keyStageTime.reqDoneTime * 1000)
                } else {
                  item.keyStageTime.reqDoneTime = "--"
                }
              }
              item.resourceVolumnNum = parseFloat(item.resourceVolumnNum / 1024).toFixed(2);
              item.severVolumnNum = parseFloat(item.severVolumnNum / 1024).toFixed(2);
              item.CpuNum = that.sumNumber(item.resourceCpuNum, item.severCpuNum).toFixed(2)
              item.RamNum = that.sumNumber(item.resourceRamNum, item.severRamNum).toFixed(2)
              item.VolumnNum = that.sumNumber(item.resourceVolumnNum, item.severVolumnNum).toFixed(2)
            })
            // that.sortTitle(data2)
            // that.sortDataByTenantName(that.allResourceList)
            that.sortDataByTime(data2.list)
            var t = JSON.stringify(data2)
            var temp = JSON.parse(t)
            that.tenantGeneral = data2
            that.allResourceList = temp.list
            that.getPageList(1)
            that.loadingSpin = false
          }
        })
      },
      // 对title排序
      sortTitle(data) {
        var key = ['集团内部', '直属单位', '省公司', '专业公司', 'IT公司内部', 'it公司内部', '外部合作方']
        var temp = []
        key.forEach(item => {
          var t = data.tenantTypeTotals.find(e => e.tenanttype == item)
          if (t != undefined) {
            temp.push(t)
          }
        })
        data.tenantTypeTotals = temp
      },
      // 对表格时间排序
      sortDataByTime(data) {
        var temp = []
        if (this.sortType == "asc") { //升序
          temp = data.sort((a, b) => {
            if (b['keyStageTime'] == null || a['keyStageTime'] == null || a['keyStageTime']['resourceDoneTime'] ==
              null ||
              b['keyStageTime'][
                'resourceDoneTime'
              ] == null) {
              console.log("sortDataByTime--项目为空时间--升序-->", b.projectId, b['project'], a.projectId, a['project'])
              return -1
            } else {
              return a['keyStageTime']['resourceDoneTime'].localeCompare(b['keyStageTime']['resourceDoneTime'])
            }
          })
          this.sortType = "desc"
        } else { //降序
          temp = data.sort((a, b) => {
            if (b['keyStageTime'] == null || a['keyStageTime'] == null || a['keyStageTime']['create_time'] ==
              null || b[
                'keyStageTime'][
                'create_time'
              ] == null) {
              console.log("sortDataByTime--项目为空时间--降序-->", b.projectId, b['keyStageTime'], a.projectId, a[
                'keyStageTime'])
              return -1
            } else {
              return b['keyStageTime']['resourceDoneTime'].localeCompare(a['keyStageTime']['resourceDoneTime'])
            }
          })
          this.sortType = "asc"
        }
        data.list = temp
      },
      sortTime() {
        this.sortDataByTime(this.allResourceList)
        this.getPageList(this.current)
      },
      sortDataByTenantName(data) {
        var temp = []
        if (this.sortTenantNameType == "asc") { //升序
          temp = data.sort((a, b) => {
            if (b['project'] == null || a['project'] == null ||
              a['project']['tenant_name'] == null || b['project']['tenant_name'] == null) {
              console.log("sortDataByTenantName--为null--->", a.projectId, a['project'], b.projectId, b['project'])
              return -1
            } else {
              let i = a['project']['tenant_name'].localeCompare(b['project']['tenant_name'])
              if (i == 0) {
                if (a['keyStageTime']['resourceDoneTime'] == null || b['keyStageTime']['resourceDoneTime'] ==
                  null) {
                  return i
                } else {
                  return a['keyStageTime']['resourceDoneTime'].localeCompare(b['keyStageTime']['resourceDoneTime'])
                }
              } else {
                return i
              }
            }
          })
          this.sortTenantNameType = "desc"
        } else { //降序
          temp = data.sort((a, b) => {
            if (b['project'] == null || a['project'] == null ||
              a['project']['tenant_name'] == null || b['project']['tenant_name'] == null) {
              console.log("sortDataByTenantName--为null--->", a.projectId, a['project'], b.projectId, b['project'])
              return -1
            } else {
              let i = b['project']['tenant_name'].localeCompare(a['project']['tenant_name'])
              if (i == 0) {
                if (a['keyStageTime']['resourceDoneTime'] == null || b['keyStageTime']['resourceDoneTime'] ==
                  null) {
                  return i
                } else {
                  return a['keyStageTime']['resourceDoneTime'].localeCompare(b['keyStageTime']['resourceDoneTime'])
                }
              } else {
                return i
              }
            }
          })
          this.sortTenantNameType = "asc"
        }
        data.list = temp
      },
      // 对租户名称排序
      slortTenantName() {
        this.sortDataByTenantName(this.allResourceList)
        this.getPageList(this.current)
      },
      // 根据类型筛选
      filterProjectType(value) {
        console.log("---------------->", value, this.filterStatus)
        var t = JSON.stringify(this.tenantGeneral)
        var base = JSON.parse(t)
        if (value.length == 0) {
          this.filterProjectTypeName = "全部"
          if (this.filterStatus == "" || this.filterStatus == '全部') {
            this.allResourceList = base.list
          } else {
            var temp = []
            base.list.forEach(item => {
              if (item.statusDes == this.filterStatus) {
                temp.push(item)
              }
            })
            this.allResourceList = temp
          }
        } else {
          this.filterProjectTypeName = value[0]
          var temp = []
          base.list.forEach(item => {

            if (this.filterStatus != "" && this.filterStatus != '全部') {
              if (item.project.projectType == this.filterProjectTypeName && item.statusDes == this.filterStatus) {
                temp.push(item)
              }
            } else {
              if (item.project.projectType == this.filterProjectTypeName) {
                temp.push(item)
              }
            }
          })
          this.allResourceList = temp
        }
        this.getPageList(1)
      },
      // 下一页
      getPageList(page) {
        var temp = this.allResourceList.slice((page - 1) * 10, page * 10)
        var t = JSON.stringify(temp)
        var t2 = JSON.parse(t)
        // t2.forEach(item => {
        // 	item.resourceVolumnNum = parseFloat(item.resourceVolumnNum / 1024).toFixed(2);
        // 	item.severVolumnNum = parseFloat(item.severVolumnNum / 1024).toFixed(2);
        // 	item.CpuNum = this.sumNumber(item.resourceCpuNum, item.severCpuNum).toFixed(2)
        // 	item.RamNum = this.sumNumber(item.resourceRamNum, item.severRamNum).toFixed(2)
        // 	item.VolumnNum = this.sumNumber(item.resourceVolumnNum, item.severVolumnNum).toFixed(2)
        // })
        this.resourceList = t2
        this.current = page
      },
      // 查看详情
      toDetail(row) {
        console.log("row----->", row)
        var t = JSON.stringify(row)
        var rowTemp = JSON.parse(t)
        if (rowTemp.resources != null && rowTemp.resources.length > 0) {
          rowTemp.resources.forEach(item => {
            if (item.resourcetype == 4 && item.val != null && item.val.indexOf('[') != -1 && typeof item.val ==
              'string') {
              item.val = JSON.parse(item.val)
            }
            if (item.unit == 'tb') {
              item.unit = 'TB'
            } else if (item.unit == 'gb') {
              item.unit = 'GB'

            }
          })
        }
        var isName = false //是否已经存在标签
        var key = row.tenantId + row.project.tenant_name
        this.tabs.list.some(item => {
          if (item.tabKey === key) {
            isName = true
            return
          }
        })

        if (!isName) {
          this.tabs.list.push({
            title: row.project.tenant_name + "租户资源订购统计",
            key: "resourcesDatail",
            tabKey: key,
            params: {
              tenantInfo: rowTemp,
              tenantName: row.project.tenant_name,
            }
          })
        }
        this.tabs.isEdit = ""
        this.tabs.lastPage = this.tabs.tabsPage
        this.tabs.tabsPage = key
        this.$store.commit("tabs", this.tabs);
      },

      filterByStatus(value) {
        console.log("---status------------->", value, this.filterProjectTypeName)
        var t = JSON.stringify(this.tenantGeneral)
        var base = JSON.parse(t)
        if (value == "") {
          this.filterStatus = "全部"
          if (this.filterProjectTypeName == '全部' || this.filterProjectTypeName == '') {
            this.allResourceList = base.list
          } else {
            var temp = []
            base.list.forEach(item => {
              if (item.project.projectType == this.filterProjectTypeName) {
                console.log("temp.push(item)---->", item)
                temp.push(item)
              }
            })
            this.allResourceList = temp
          }

        } else {
          this.filterStatus = value[0]
          var temp = []
          base.list.forEach(item => {
            if (this.filterProjectTypeName != '全部') {
              if (item.project.projectType == this.filterProjectTypeName && item.statusDes == this.filterStatus) {
                temp.push(item)
              }
            } else {
              if (item.statusDes == this.filterStatus) {
                temp.push(item)
              }
            }
          })
          this.allResourceList = temp
        }

        this.getPageList(1)
      },
      //刷新
      refresh() {
        this.searchInfo = {
          tenantName: "",
          projectName: ""
        }
        this.filterProjectTypeName = "全部"
        this.filterStatus = "全部"
        this.allResourceList = this.tenantGeneral.list
        this.getPageList(1)
      },
      // 查找
      search() {
        console.log(this.searchInfo, )
        if (this.searchInfo.tenantName != "" || this.searchInfo.projectName != "") {
          this.allResourceList = []
          this.tenantGeneral.list.forEach(item => {
            if (!this.utils.objectIsEmpty(item.project) && !this.utils.objectIsEmpty(item.project.tenant_name)) {
              if (this.searchInfo.tenantName != '' && this.searchInfo.projectName == '' && item.project.tenant_name
                .indexOf(
                  this.searchInfo.tenantName) != -1) {
                this.allResourceList.push(item)
              } else if (this.searchInfo.projectName != '' && this.searchInfo.tenantName == '' && item.project
                .project
                .indexOf(this.searchInfo.projectName) != -1) {
                this.allResourceList.push(item)
              } else if (this.searchInfo.projectName != '' && this.searchInfo.tenantName != '' &&
                item.project.tenant_name.indexOf(this.searchInfo.tenantName) != -1 &&
                item.project.project.indexOf(this.searchInfo.projectName) != -1) {
                console.log("----------------")
                this.allResourceList.push(item)
              }
            }
            this.getPageList(1)
          })
        }
      },
      sumNumber(v1, v2) {
        return parseFloat(v1) + parseFloat(v2)
      },
      formaTime(value) {
        return this.utils.formatDate(new Date(value), "yyyy-MM-dd")
      }
    }

  }
</script>
